{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# LOGISTIC REGRESSION WITH CUSTOM DATA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Packages loaded\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import matplotlib.pyplot as plt\n",
    "print (\"Packages loaded\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['trainlabel', 'imgsize', 'trainimg', 'testimg', 'testlabel', 'use_gray']\n",
      "52 train images loaded\n",
      "35 test images loaded\n",
      "4096 dimensional input\n",
      "2 classes\n"
     ]
    }
   ],
   "source": [
    "# Load them!\n",
    "cwd = os.getcwd()\n",
    "loadpath = cwd + \"/data/custom_data.npz\"\n",
    "l = np.load(loadpath)\n",
    "\n",
    "# See what's in here\n",
    "print (l.files)\n",
    "\n",
    "# Parse data\n",
    "trainimg = l['trainimg']\n",
    "trainlabel = l['trainlabel']\n",
    "testimg = l['testimg']\n",
    "testlabel = l['testlabel']\n",
    "use_gray = l['use_gray']\n",
    "ntrain = trainimg.shape[0]\n",
    "nclass = trainlabel.shape[1]\n",
    "dim    = trainimg.shape[1]\n",
    "ntest  = testimg.shape[0]\n",
    "print (\"%d train images loaded\" % (ntrain))\n",
    "print (\"%d test images loaded\" % (ntest))\n",
    "print (\"%d dimensional input\" % (dim))\n",
    "print (\"%d classes\" % (nclass))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define network"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "tf.set_random_seed(0)\n",
    "# Parameters of Logistic Regression\n",
    "learning_rate   = 0.001\n",
    "training_epochs = 1000\n",
    "batch_size      = 10\n",
    "display_step    = 100\n",
    "\n",
    "# Create Graph for Logistic Regression\n",
    "x = tf.placeholder(\"float\", [None, dim]) \n",
    "y = tf.placeholder(\"float\", [None, nclass]) \n",
    "W = tf.Variable(tf.zeros([dim, nclass]), name = 'weights')\n",
    "b = tf.Variable(tf.zeros([nclass]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Define functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Functions ready\n"
     ]
    }
   ],
   "source": [
    "WEIGHT_DECAY_FACTOR = 1 #  0.000001\n",
    "l2_loss = tf.add_n([tf.nn.l2_loss(v) \n",
    "            for v in tf.trainable_variables()])\n",
    "_pred = tf.nn.softmax(tf.matmul(x, W) + b) \n",
    "cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(_pred)\n",
    "                                     , reduction_indices=1)) \n",
    "cost = cost + WEIGHT_DECAY_FACTOR*l2_loss\n",
    "optm = tf.train.GradientDescentOptimizer(\n",
    "    learning_rate).minimize(cost) \n",
    "_corr = tf.equal(tf.argmax(_pred, 1), tf.argmax(y, 1))    \n",
    "accr = tf.reduce_mean(tf.cast(_corr, tf.float32))\n",
    "init = tf.initialize_all_variables()\n",
    "print (\"Functions ready\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Optimize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch: 000/1000 cost: 0.626751423\n",
      " Training accuracy: 0.600\n",
      " Test accuracy: 0.686\n",
      "Epoch: 100/1000 cost: 0.438359487\n",
      " Training accuracy: 0.800\n",
      " Test accuracy: 0.714\n",
      "Epoch: 200/1000 cost: 0.367508155\n",
      " Training accuracy: 0.900\n",
      " Test accuracy: 0.686\n",
      "Epoch: 300/1000 cost: 0.363990796\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.714\n",
      "Epoch: 400/1000 cost: 0.406193763\n",
      " Training accuracy: 0.900\n",
      " Test accuracy: 0.714\n",
      "Epoch: 500/1000 cost: 0.400928861\n",
      " Training accuracy: 0.900\n",
      " Test accuracy: 0.714\n",
      "Epoch: 600/1000 cost: 0.366956294\n",
      " Training accuracy: 0.900\n",
      " Test accuracy: 0.686\n",
      "Epoch: 700/1000 cost: 0.334192312\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.686\n",
      "Epoch: 800/1000 cost: 0.392353541\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.714\n",
      "Epoch: 900/1000 cost: 0.383330226\n",
      " Training accuracy: 1.000\n",
      " Test accuracy: 0.714\n",
      "Optimization Finished!\n"
     ]
    }
   ],
   "source": [
    "# Launch the graph\n",
    "sess = tf.Session()\n",
    "sess.run(init)\n",
    "# Training cycle\n",
    "for epoch in range(training_epochs):\n",
    "    avg_cost = 0.\n",
    "    num_batch = int(ntrain/batch_size)\n",
    "    # Loop over all batches\n",
    "    for i in range(num_batch): \n",
    "        randidx = np.random.randint(ntrain, size=batch_size)\n",
    "        batch_xs = trainimg[randidx, :]\n",
    "        batch_ys = trainlabel[randidx, :]                \n",
    "        # Fit training using batch data\n",
    "        sess.run(optm, feed_dict={x: batch_xs, y: batch_ys})\n",
    "        # Compute average loss\n",
    "        avg_cost += sess.run(cost\n",
    "            , feed_dict={x: batch_xs, y: batch_ys})/num_batch\n",
    "\n",
    "    # Display logs per epoch step\n",
    "    if epoch % display_step == 0:\n",
    "        print (\"Epoch: %03d/%03d cost: %.9f\" % \n",
    "               (epoch, training_epochs, avg_cost))\n",
    "        train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys})\n",
    "        print (\" Training accuracy: %.3f\" % (train_acc))\n",
    "        test_acc = sess.run(accr, feed_dict={x: testimg, y: testlabel})\n",
    "        print (\" Test accuracy: %.3f\" % (test_acc))\n",
    "print (\"Optimization Finished!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CLOSE SESSION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Session closed.\n"
     ]
    }
   ],
   "source": [
    "sess.close()\n",
    "print (\"Session closed.\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
